import {
  allotInWarehouseSign,
  allotWarehouseInOrOutFormSave,
  checkAllotOutWarehouse,
  checkWarehouseGoodSnCode,
  getAllotInWarehouseDetail,
  getAllotInWarehouseDetailMaterialList,
  getAllotInWarehouseList,
  getAllotOutWarehouseList,
  getAllWarehouseOptionList,
  getCurrentAccountsNetworkInWarehouse,
  getNetworkWarehouse,
  getStorePageSelect,
  updateAllotInWarehouse,
  updateLogisticsByIds,
} from '@/api/store'
import { ref } from 'vue'

/**
 * 获取部门仓库
 */
export const useGetNetworkWarehouse = () => {
  // 获取仓库加载
  const getWarehouseLoading = ref(false)

  // 仓库信息
  const warehouse = ref<WarehouseByNetowrkId>({
    address: '',
    area: '',
    city: '',
    enableStore: false,
    province: '',
    storeId: '',
    storeName: '',
    userPhone: '',
    username: '',
  })

  /**
   * 获取仓库
   * @param id - 部门id
   */
  const getWarehouse = async (id: string) => {
    getWarehouseLoading.value = true
    const res = await getNetworkWarehouse(id)
    warehouse.value = res.data.data.row
    getWarehouseLoading.value = false
  }

  return {
    warehouse,
    getWarehouseLoading,
    getWarehouse,
  }
}

/**
 * 获取仓库下拉数据
 */
export const useGetWarehouseOptionList = () => {
  // 获取仓库下拉数据加载
  const getWarehouseOptionListLoading = ref(false)

  // 获取仓库下拉数据请求参数
  const getWarehouseOptionListParam = ref({
    name: '',
    page: 1,
    limit: 100,
    id: '',
  })

  // 仓库下拉数据
  const warehouseOptionList = ref<Array<WarehouseOption>>([])

  /**
   * 获取下拉数据
   */
  const getWarehouseOptionList = async () => {
    getWarehouseOptionListLoading.value = true
    const res = await getStorePageSelect(getWarehouseOptionListParam.value)
    warehouseOptionList.value = res.data.data.rows
    getWarehouseOptionListLoading.value = false
  }

  return {
    warehouseOptionList,
    getWarehouseOptionListParam,
    getWarehouseOptionListLoading,
    getWarehouseOptionList,
  }
}

/**
 * 旧件批量返仓
 */
export const useOldPartMultipleReturn = () => {
  // 提交加载
  const submitLoading = ref(false)

  // 提交表单
  const submitForm = ref<StorageAPIRequest.OldPartMultipleReturn>({
    ids: [],
    logisticsCode: '',
    logisticsCorpName: '',
  })

  /**
   * 提交方法
   */
  const submit = async () => {
    submitLoading.value = true
    const res = await updateLogisticsByIds(submitForm.value)
    submitLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    submitForm.value = {
      ids: [],
      logisticsCode: '',
      logisticsCorpName: '',
    }
  }

  return {
    submitForm,
    submitLoading,
    submit,
    resetForm,
  }
}

/**
 * 获取所有的仓库下拉选项
 */
export const useGetAllWarehouseOptionList = () => {
  // 获取下拉选项加载
  const getOptionListLoading = ref(false)

  // 获取下拉选项请求参数
  const getOptionListParam = ref({
    id: '',
    name: '',
    page: 1,
    limit: 1000,
  })

  // 下拉选项列表
  const optionList = ref<Array<AllWarehouseOption>>([])

  /**
   * 获取下拉选项
   */
  const getOptionList = async () => {
    getOptionListLoading.value = true
    const res = await getAllWarehouseOptionList(getOptionListParam.value)
    getOptionListLoading.value = false
    if (res.data.code === 20000) {
      optionList.value = res.data.data.rows
      return ''
    }
    return res.data.message
  }

  return {
    optionList,
    getOptionListParam,
    getOptionListLoading,
    getOptionList,
  }
}

/**
 * 调拨出-入库表单保存
 */
export const useAllotWarehouseInOrOutFormSave = () => {
  // 表单保存加载
  const formSaveLoading = ref(false)

  // 表单
  const form = ref<StorageAPIRequest.AllotWarehouseInOrOutForm>({
    address: '',
    area: '',
    city: '',
    delMaterialIds: [],
    phone: '',
    province: '',
    remark: '',
    storeMaterialVos: [],
    typeId: '',
    typeName: '',
    username: '',
  })

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      address: '',
      area: '',
      city: '',
      delMaterialIds: [],
      phone: '',
      province: '',
      remark: '',
      storeMaterialVos: [],
      typeId: '',
      typeName: '',
      username: '',
    }
  }

  /**
   * 提交事件
   * @param {1|2} type - 调拨类型：2-调拨入库单， 3-调拨出库单
   * @param {0|1} submitType 提交类型：0-提交，1-仅保存
   * @returns - 提交结果
   */
  const submit = async (type: 2 | 3, submitType: 0 | 1) => {
    formSaveLoading.value = true
    const res = await allotWarehouseInOrOutFormSave(form.value, type, submitType)
    formSaveLoading.value = false
    return res.data
  }

  return {
    form,
    formSaveLoading,
    submit,
    resetForm,
  }
}

/**
 * 调拨校验序列号
 */
export const useAllotCheckSnCode = () => {
  // 校验表单
  const form = ref<StorageAPIRequest.AllotCheckSnCodeForm>({
    storeGoodsVos: [],
  })

  // 校验加载
  const checkLoading = ref(false)

  /**
   * 校验
   */
  const check = async () => {
    checkLoading.value = true
    const res = await checkWarehouseGoodSnCode(form.value)
    checkLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value.storeGoodsVos.length = 0
  }

  return {
    form,
    checkLoading,
    check,
    resetForm,
  }
}

/**
 * 获取调拨入库列表
 */
export const useGetAllotInWarehouseList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref<StorageAPIRequest.GetAllotInWarehouseListParam>({
    dateType: '',
    endTime: '',
    limit: 0,
    page: 0,
    receiptStoreIds: [],
    search: '',
    source: '',
    startTime: '',
    status: '',
    storeIds: [],
    typeId: '',
    // submitType: '',
    type: '',
  })

  // 列表
  const list = ref<Array<AllotInWarehouse>>([])

  // 列表总条数
  const listTotal = ref(0)

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await getAllotInWarehouseList(getListParam.value)
    getListLoading.value = false
    list.value = res.data.data.rows
    listTotal.value = Number(res.data.data.total)
  }

  return {
    list,
    listTotal,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 获取调拨入库详情
 */
export const useGetAllotInWarehouseDetail = () => {
  // 获取详情加载
  const getDetailLoading = ref(false)

  // 入库详情
  const detail = ref<WarehouseAllotInWarehouseDetail>({
    cancelEmp: null,
    cancelRemark: null,
    cancelTime: null,
    createEmp: '',
    createTime: '',
    estimateTime: null,
    id: '',
    logisticsName: null,
    logisticsNo: null,
    logisticsRemark: null,
    outboundNo: '',
    receiptAddrType: null,
    receiptAddress: null,
    receiptArea: '',
    receiptCity: '',
    receiptName: '',
    receiptNo: null,
    receiptPhone: '',
    receiptProvince: '',
    receiptStore: null,
    receiptTown: null,
    rejectEmp: null,
    rejectRemark: null,
    rejectTime: null,
    relationId: null,
    remark: '',
    reverseWritingFailReason: null,
    reverseWritingStatus: null,
    reviewEmp: null,
    reviewRemark: null,
    reviewTime: null,
    source: 1,
    status: 1,
    storePositionId: null,
    storeType: 0,
    submitType: 1,
    type: '',
    typeId: '',
    updateEmp: '',
    updateTime: '',
    workId: null,
    workNo: null,
  })

  /**
   * 获取详情
   * @param {string} id - 调拨入库id
   */
  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await getAllotInWarehouseDetail(id)
    getDetailLoading.value = false
    if (res.data.code === 20000) {
      detail.value = res.data.data
      return ''
    }
    return res.data.message
  }

  return {
    detail,
    getDetailLoading,
    getDetail,
  }
}

/**
 * 获取入库单详情的物料列表
 */
export const useGetAllotInWarehouseMaterialList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 物料列表
  const materialList = ref<Array<AllotInWarehouseDetailMaterial>>([])

  /**
   * 获取物料列表
   * @param {string} id - 入库单id
   */
  const getList = async (id: string) => {
    getListLoading.value = true
    const res = await getAllotInWarehouseDetailMaterialList(id)
    getListLoading.value = false
    materialList.value = res.data.data
  }

  return {
    materialList,
    getListLoading,
    getList,
  }
}

/**
 * 更新入库单
 */
export const useUpdateAllotInWarehouse = () => {
  // 更新加载
  const updateLoading = ref(false)

  /**
   * 更新表单
   */
  const updateForm = ref<StorageAPIRequest.AllotWarehouseInOrOutForm>({
    address: '',
    area: '',
    city: '',
    delMaterialIds: [],
    phone: '',
    province: '',
    remark: '',
    storeMaterialVos: [],
    typeId: '',
    typeName: '',
    username: '',
  })

  /**
   * 更新
   * @param {string} id - 入库单id
   * @param {0|1} submitType - 提交类型：0-提交，1-仅保存
   */
  const update = async (id: string, submitType: 0 | 1) => {
    updateLoading.value = true
    const res = await updateAllotInWarehouse(updateForm.value, id, submitType)
    updateLoading.value = false
    return res.data
  }

  return {
    updateForm,
    updateLoading,
    update,
  }
}

/**
 * 调拨出库审核
 */
export const useCheckAllotOutWarehouse = () => {
  // 审核加载
  const checkLoading = ref(false)

  // 审核表单
  const checkForm = ref<StorageAPIRequest.CheckAllotOutWarehouse>({
    address: '',
    checkRemark: '',
    checkResult: 1,
    area: '',
    city: '',
    phone: '',
    province: '',
    remark: '',
    storeMaterialVos: [],
    typeId: '',
    typeName: '',
    username: '',
  })

  /**
   * 审核
   * @param {string} id - 出库单id
   */
  const check = async (id: string) => {
    checkLoading.value = true
    const res = await checkAllotOutWarehouse(id, checkForm.value)
    checkLoading.value = false
    return res.data
  }

  const resetForm = () => {
    checkForm.value = {
      address: '',
      checkRemark: '',
      checkResult: 1,
      area: '',
      city: '',
      phone: '',
      province: '',
      remark: '',
      storeMaterialVos: [],
      typeId: '',
      typeName: '',
      username: '',
    }
  }

  return {
    checkForm,
    checkLoading,
    check,
    resetForm,
  }
}

/**
 * 获取当前账号所在部门的部门信息和仓库信息
 */
export const useGetAccountNetworkInfoAndInWarehouse = () => {
  // 获取加载
  const getCurrentAccountsNetworkInWarehouseInfoLoading = ref(false)

  // 信息
  const currentAccountsNetworkInWarehouseInfo = ref<WarehouseCurrentAccountsNetworkInWarehouse>({
    inStrore: '',
    inStroreId: '',
    networkAddress: '',
    networkArea: '',
    networkCity: '',
    networkId: '',
    networkManagerName: '',
    networkManagerPhone: '',
    networkName: '',
    networkProvince: '',
  })

  /**
   * 获取信息
   */
  const getCurrentAccountsNetworkInWarehouseInfo = async () => {
    getCurrentAccountsNetworkInWarehouseInfoLoading.value = true
    const res = await getCurrentAccountsNetworkInWarehouse()
    getCurrentAccountsNetworkInWarehouseInfoLoading.value = false
    if (res.data.code === 20000) {
      currentAccountsNetworkInWarehouseInfo.value = res.data.data
      return ''
    }
    return res.data.message
  }

  return {
    currentAccountsNetworkInWarehouseInfo,
    getCurrentAccountsNetworkInWarehouseInfoLoading,
    getCurrentAccountsNetworkInWarehouseInfo,
  }
}

/**
 * 获取调拨出库列表
 */
export const useGetAllotOutWarehouseList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref<StorageAPIRequest.GetAllotOutWarehouseListParam>({
    dateType: '',
    endTime: '',
    limit: 50,
    page: 1,
    receiptStoreIds: [],
    search: '',
    source: '',
    startTime: '',
    status: '',
    storeIds: [],
    type: '',
  })

  // 列表总条数
  const listTotal = ref(0)

  // 出库列表
  const list = ref<Array<AllotOutWarehouseItem>>([])

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await getAllotOutWarehouseList(getListParam.value)
    getListLoading.value = false
    if (res.data.code === 20000) {
      list.value = res.data.data.rows
      listTotal.value = Number(res.data.data.total)
      return ''
    }
    return res.data.message
  }

  return {
    list,
    listTotal,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 调拨入库签收
 */
export const useAllotInWarehouseSign = () => {
  // 签收加载
  const signLoading = ref(false)

  // 签收表单
  const signForm = ref<{ ids: Array<string>; remark: string }>({
    ids: [],
    remark: '',
  })

  /**
   * 签收
   */
  const sign = async () => {
    signLoading.value = true
    const res = await allotInWarehouseSign(signForm.value)
    signLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    signForm.value = {
      ids: [],
      remark: '',
    }
  }

  return {
    signForm,
    signLoading,
    sign,
    resetForm,
  }
}
